<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use common\helpers\DependencyFileHelper;

/**
 * This is the model class for table "{{%friend_link}}".
 *
 * @property string $id 友情链接id
 * @property string $name 友情链接名字
 * @property string $image 友情链接图片
 * @property string $url 友情链接网址
 * @property string $target 跳转方式
 * @property string $sort 排序
 * @property int $status 状态
 * @property string $remark 备注
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class FriendLink extends \yii\db\ActiveRecord
{
    const STATUS_YES = 1;
    const STATUS_NO = 0;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%friend_link}}';
    }

    /*
     * 通过行为自动使时间创建和更新
     * */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'url'], 'required'],
            [['sort', 'status'], 'integer'],
            [['name', 'image', 'url', 'target'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('database', 'ID'),
            'name' => Yii::t('database', 'Name'),
            'image' => Yii::t('database', 'Image'),
            'url' => Yii::t('database', 'Url'),
            'target' => Yii::t('database', 'Target'),
            'sort' => Yii::t('database', 'Sort'),
            'status' => Yii::t('database', 'Status'),
            'remark' => Yii::t('database', 'Remark'),
            'created_at' => Yii::t('database', 'Created At'),
            'updated_at' => Yii::t('database', 'Updated At'),
        ];
    }

    /**
     * @param bool $insert
     * @param array $changedAttributes
     * @throws \yii\base\InvalidConfigException
     *
     * @var $dependencyFileHelper \common\helpers\DependencyFileHelper
     */
    public function afterSave($insert, $changedAttributes)
    {
        //更新缓存依赖文件
        $dependencyFileHelper = Yii::createObject([
                'class' => DependencyFileHelper::className(),
                'dependencyFileName' => 'siteAllInfo.txt',
            ]
        );

        $dependencyFileHelper->updateDependencyFile();

        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
    }
}
